package dev.xesam.bottomsheet.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Arrays;
import java.util.List;

import dev.xesam.bottomsheet.BottomSheetItem;
import dev.xesam.bottomsheet.R;

public class MenuAdapter extends BaseAdapter {

    protected Context context;
    protected List<BottomSheetItem> items;
    private int menuType = 0;


    public MenuAdapter(Context context, BottomSheetItem[] items,int menuType) {
        this.context = context;
        this.items = Arrays.asList(items);
        this.menuType = menuType;
    }
    public MenuAdapter(Context context, List<BottomSheetItem> items,int menuType) {
        this.context = context;
        this.items = items;
        this.menuType = menuType;
    }
    public MenuAdapter(Context context, List<BottomSheetItem> items) {
        this(context,items,0);
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public BottomSheetItem getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return items.get(position).getItemId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if(menuType ==0){
                view = LayoutInflater.from(context).inflate(R.layout.menu_vertical_item_view, parent, false);
                holder.prefix = view.findViewById(R.id.vertical_menu_prefix);
                holder.content = view.findViewById(R.id.vertical_menu_content);
            }else {
                view = LayoutInflater.from(context).inflate(R.layout.menu_horizontal_item_view, parent, false);
                holder.prefix = view.findViewById(R.id.horizontal_menu_prefix);
                holder.content = view.findViewById(R.id.horizontal_menu_content);
            }
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        BottomSheetItem item = getItem(position);

        if (holder.prefix != null) {

            bindPrefix(holder.prefix, position, item);
        }

        if (holder.content != null) {
            bindContent(holder.content, position, item);
        }
        return view;
    }

    protected void bindPrefix(View view, int position, BottomSheetItem item) {
        ((ImageView) view).setImageResource(item.getIconId());
    }


    protected void bindContent(View view, int position, BottomSheetItem item) {
        ((TextView) view).setText(item.getContent());
    }


    private class ViewHolder {
        View prefix;
        View content;
    }
}